//
//  DealDetail.m
//  BrushFace
//
//  Created by timer_q on 16/1/27.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "PopularDetailVC.h"
#import "PopularViewCell.h"

@interface PopularDetailVC ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataArray;
    NSInteger   _pageNum;
    NSInteger    _pageSize;
}
@end

@implementation PopularDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self UIConfig];
    [self setRefresh];
    [self.navBar setTitle:@"积分明细"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
    _dataArray = [[NSMutableArray alloc] init];
    _pageNum = 0;
    _pageSize = 10;
    [self requestData];
}
- (void)UIConfig{
    self.view.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, SCREEN_WIDTH, SCREEN_HEIGHT-65) style:UITableViewStylePlain];
    _tableView.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -tableviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PopularViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    if (cell == nil) {
        cell = [PopularViewCell loadBundleNib];
    }
    cell.dateLabel.text = [self timerStrWithTime:[NSString stringWithFormat:@"%@",[[_dataArray objectAtIndex:indexPath.row] objectForKey:@"tradetime"]]];
    if ([[NSString stringWithFormat:@"%@",[[_dataArray objectAtIndex:indexPath.row] objectForKey:@"type"]] integerValue]==1) {
        cell.titleLabel.text = @"好友授信";
    }else{
        cell.titleLabel.text = @"完善资料";
    }
    cell.addLabel.text = [NSString stringWithFormat:@"+%@",[[_dataArray objectAtIndex:indexPath.row] objectForKey:@"amount"]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
- (NSString *)timerStrWithTime:(NSString *)time{
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:[time doubleValue]/1000+28800];
    NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"YYYY.MM.dd HH:MM:SS"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}

- (void)requestData{
    NSString *requestStr = [NSString stringWithFormat:@"%@/user/getpopularotylog4app.json",SERVICE_ADRESS];
    NSDictionary *params = @{@"curPage":[NSString stringWithFormat:@"%ld",_pageNum],@"rPP":[NSString stringWithFormat:@"%ld",_pageSize]};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsondict = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        [_dataArray removeAllObjects];
        [_dataArray addObjectsFromArray:[jsondict objectForKey:@"data"]];
        [_tableView reloadData];
        if ([jsondict[@"data"] count]==0) {
            UILabel *footLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
            footLabel.text = @"暂无人气";
            footLabel.textColor = [UIColor grayColor];
            footLabel.textAlignment = NSTextAlignmentCenter;
            footLabel.font = [UIFont systemFontOfSize:20];
            _tableView.tableFooterView = footLabel;
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
}
#pragma mark - 上拉下拉
- (void)setRefresh{
    @weakify (self);
    [_tableView addPullToRefreshWithActionHandler:^{
        @strongify (self);
        _pageSize = 10;
        _pageNum = 0;
        [self requestData];
        [self->_tableView endAllRefreshingWithEnd:NO];
    }];
    
    [_tableView addInfiniteScrollingWithActionHandler:^{
        @strongify (self);
        _pageNum = _pageNum+1;
        NSString *requestStr = [NSString stringWithFormat:@"%@/user/getpopularotylog4app.json",SERVICE_ADRESS];
        NSDictionary *params = @{@"curPage":[NSString stringWithFormat:@"%ld",self->_pageNum],@"rPP":[NSString stringWithFormat:@"%ld",self->_pageSize]};
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            NSDictionary *jsondict = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
            if ([[jsondict objectForKey:@"data"] count]<10) {
                [self->_tableView endAllRefreshingWithEnd:YES];
            }else{
                [self->_tableView endAllRefreshingWithEnd:NO];
            }
            [self->_dataArray addObjectsFromArray:[jsondict objectForKey:@"data"]];
            [self->_tableView reloadData];
            
        } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
            [self->_tableView endAllRefreshingWithEnd:NO];
        }];
    }];
    
}

@end
